package com.zdx.yswl.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import com.loopj.android.image.SmartImageView;
import com.zdx.yswl.R;
import com.zdx.yswl.tool.KNetTool;
import com.zdx.yswl.ui.bean.BannerBean;
import com.zdx.yswl.ui.bean.HeadImgBean;
import com.zdx.yswl.ui.bean.PictruesBean;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Toast;

/**
 * 楼盘 主页banner
 * 
 * @author Administrator
 */

public class PicturesViewPagerAdapter extends PagerAdapter {

	public interface OnClickPagerListener {
		public void onClikPager(View v);
	};

	private OnClickPagerListener mClickListener;
	private List<View> mView = new ArrayList<View>();
	ArrayList<HeadImgBean> listData ;
	private Context context;

	public PicturesViewPagerAdapter(final Context context) {
		super();
		this.context = context;
	}

	public void setData(PictruesBean data) {
		ArrayList<HeadImgBean> list = new ArrayList<HeadImgBean>();
		int alenth = data.aroundPicList.size();
		int elenth = data.effectPicList.size();
		int rlenth = data.realPicList.size();
		int slenth = data.samplePicList.size();
		
		for (int i = 0; i < elenth; i++) {
			list.add(data.effectPicList.get(i));
		}
		for (int i = 0; i < slenth; i++) {
			list.add(data.samplePicList.get(i));
		}
		for (int i = 0; i < rlenth; i++) {
			list.add(data.realPicList.get(i));
		}
		for (int i = 0; i < alenth; i++) {
			list.add(data.aroundPicList.get(i));
		}

		// 默认图片
		LayoutInflater inflater = LayoutInflater.from(context);
		for (int i = 0; i < list.size(); i++) {
			View view = inflater.inflate(R.layout.viewpager_item, null);
			SmartImageView image = (SmartImageView) view.findViewById(R.id.image);
			image.setImageResource(R.drawable.aio_image_default_round);
			mView.add(view);
		}
		listData = list;
	}

	@Override
	public int getCount() {
		if (mView != null && mView.size() > 0) {
			return mView.size();
		} else {
			return 0;
		}
		// return Integer.MAX_VALUE;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView(mView.get(position));
		// position % urls.length 取余数
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {

		int num = container.getChildCount();
		for (int i = 0; i < num; i++) {
			if (container.getChildAt(i).hashCode() == mView.get(position).hashCode()) {
				container.removeView(mView.get(position ));
				break;
			}
		}
		View view = mView.get(position );
		SmartImageView mImage = (SmartImageView) view.findViewById(R.id.image);
		if (KNetTool.isNetworkAvalible(context)) {
			mImage.setImageUrl(listData.get(position).url);
		}
		view.setTag(position);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mClickListener!= null) {
					mClickListener.onClikPager(v);
				}
			}
		});

		container.addView(view);
		return view;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	public void setAdaperOnClickListener(OnClickPagerListener listener) {
		this.mClickListener = listener;
	}
	
}